#define _CRT_SECURE_NO_WARNINGS 1
/**
*      author: Y_Yao
*      created: 2024-12-06 20:50:55
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int n, m;
vector<vector<int>> v(N);
int ans = INF;
bool vis[N];
int flag;

void dfs(int cur, int len)
{
    if (cur == 1 && len > 0)
    {
        flag = 1;
        ans = min(ans, len);
        return;
    }

    for (int i = 0;i < v[cur].size();i++)
    {
        if (vis[v[cur][i]]) continue;
        vis[v[cur][i]] = true;
        dfs(v[cur][i], len + 1);
        vis[v[cur][i]] = false;
    }
}

void solve()
{
    cin >> n >> m;
    for (int i = 1;i <= m;i++)
    {
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);//a to b
    }

    dfs(1, 0);
    if (flag == 0) ans = -1;
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}